<%@ page isELIgnored ="false" %>
<%@ page language="java" pageEncoding="UTF-8"%>
<html> 
<head>
<meta http-equiv="Content-Type" content="text/jspl" charset="utf-8">

<%
String path = request.getContextPath();
pageContext.setAttribute("path",path);
pageContext.setAttribute("prefix","_editEdit_");
%>

<script> 

function ${prefix}editUser()
{
	var data ={};
	data["name"]=document.getElementById("${prefix}name").value;
	data["memo"]=document.getElementById("${prefix}memo").value;
	data["nonLocked"]=document.getElementById("${prefix}nonLocked").checked?1:0;

	data["realName"]=document.getElementById("${prefix}realName").value;


	data["email"]=document.getElementById("${prefix}email").value;
	data["phone"]=document.getElementById("${prefix}phone").value;
	data["deleteFlag"]=false;
	
	var fakeRoles=[];
	var nodes = document.getElementsByName("${prefix}role");
	for (var i=0;i<nodes.length ;++i )
	{
		if (nodes[i].checked==true){
		fakeRoles.push({"id":nodes[i].value})
		}
	}
	data["fakeRoles"]=fakeRoles;
	

	HMSRest.AjaxPut("${path}/service/user/${param['userId']}",dojo.toJson(data),function(response,ioArgs){
		if (response.errorCode==0){
			alert("修改用户成功！");
		}else{
			alert(response.errorDesc);
		}
		});
	 return true; 
}

function ${prefix}getAllRole()
{
	HMSRest.AjaxPost("${path}/service/roles","{}",function (response,ioArgs)
		{
		var cols = 2;
		var i=0;
		var innerHtml ="<table align=\"center\" id=\"tblRoles\"  border=\"1\" cellspacing=\"0\" cellpadding=\"0\">";
		var data = response.items;
		for (var i=0;i<response.count;++i){
			if (i%cols == 0)
			{
				innerHtml += "<tr>";
			}
		innerHtml += "<td>" + data[i].name +"<input type='checkbox' name='${prefix}role' value=\""+data[i].id+"\" /> </td>";
		if (i%cols == cols-1)
		{
			innerHtml += "</tr>";
		}
		}
		var mod = i%cols;
		if (mod >0 )
		{
			for (i=0;i<mod-1 ;++i )
			{
			innerHtml += "<td>&nbsp;</td>";
			}
		}
		innerHtml += "</tr>";
		innerHtml +="</table>";
		document.getElementById("${prefix}roles").innerHTML=innerHtml;
		}
		,function(error,ioArgs){}
	);
}

function ${prefix}fillDataIntoForm()
{
	
	HMSRest.AjaxGet("${path}/service/user/fetch/${param['userId']}",function(response,ioArgs){
	   document.getElementById("${prefix}name").value=response.name;
	   document.getElementById("${prefix}realName").value=response.realName;
	   document.getElementById("${prefix}email").value=response.email;
	   document.getElementById("${prefix}phone").value=response.phone;
	   document.getElementById("${prefix}memo").value=response.memo;

	   if (response.nonLocked=1)
		 {
			 document.getElementById("${prefix}nonLocked").checked=true;
		 }else{
			  document.getElementById("${prefix}nonLocked").checked=false;
		 }
		 
		/*
	   if (response.nonLocked=1)
	   {
		 document.getElementById("nonLocked").checked=true;
	   }else{
		  document.getElementById("nonLocked").checked=false;
	   }
	   */
	   var nodes = document.getElementsByName("${prefix}role");
	   var rolesData = response.fakeRoles;
	   for (var i=0;i<nodes.length ;++i )
	   {
		 var roleId = nodes[i].value;
		 for (var j=0;j<rolesData.length;++j )
		 {
			if (roleId == rolesData[j].id)
			{
				nodes[i].checked=true;
			}
		 }
	  }
	 }
	 ,function(error,ioArgs){}
	 );
}
</script> 
</head>
<body> 
<form id="roleInfo"  action="${path}/service/user" method="POST"> 
<TABLE width="400px"  border="0" cellspacing="0" cellpadding="0" align="center">
<tr><td align="left">基本信息:</td></tr>
<tr><td>
<TABLE  border="1" cellspacing="0" cellpadding="0" align="center">
<TR>
	<TD align="right">用户名:&nbsp;</TD><TD>&nbsp;<input type="textfield" name="name" id="${prefix}name" readOnly="true"/></TD>
</TR>
<TR>
	<TD align="right">真实姓名:&nbsp;</TD><TD>&nbsp;<input type="textfield" name="realName" id="${prefix}realName"/></TD>

</TR>
<TR>
	<TD align="right">是否开锁:&nbsp;</TD><TD>&nbsp;<input type="checkbox" name="nonLocked" id="${prefix}nonLocked" />  </TD>
</TR>
<TR>
	<TD align="right">EMail:&nbsp;</TD><TD>&nbsp;<input type="textfield" name="email" id="${prefix}email" />  </TD>
</TR>
<TR>
	<TD align="right">电话号码:&nbsp;</TD><TD>&nbsp;<input type="textfield" name="phone" id="${prefix}phone" />  </TD>
</TR>
<TR>
	<TD  align="right">备注:&nbsp;</TD><TD>&nbsp;<input type="textfield" name="memo" length="32" id="${prefix}memo"/></TD>
</TR>
</TABLE>
</td></tr>
<tr><td>角色选择:</td></tr>
<tr><td>
<div id="${prefix}roles" >
	
</div>
</td></tr>
<tr><td align="center"><input type="button" name="sub" value="保存" onclick="${prefix}editUser();"/></td></tr>
</table>
</form>

<script>
${prefix}getAllRole();
${prefix}fillDataIntoForm();
</script> 
</body>
</html>

